<?php
namespace MyClass;
Class Common extends \C\Action{
	protected function _bgin(){
		if($_REQUEST['Openid']) {
			$this->login_Wx($_REQUEST['Openid']);
		}
		$user = session("UserInfo");
		$this->s('_user',$user);
		$this->GetLink();
		$this->GetSee();
		$this->GetCompany();
	}
	public function login_Wx($open) {
		//匹配用户
		$w['OpenId'] = $open;
		$info=$this->m('user')->where($w)->find();
		if($info){
			if(isset($info['Cid'])){
				$company = $this->m('company')->where("Id = " . $info['Cid'])->find();
			}else{
				$company['CompanyName']="";
			}
			$user = array(
				'id' => $info['Id'],
				'cid' => $info['Cid'],
				'pid' => $info['Pid'],
				'phone' => $info['Phone'],
				'CompanyName' => $company['CompanyName'],
				'UserPhoto' => $info['UserPhoto'],
			);
			session('UserInfo',$user);
		}
	}
	public function GetLink(){
		$list = $this->m('link')->order('Sort DESC')->select();
		$this->s('link',$list);
	}
	public function GetSee(){
		if($_SESSION['UserInfo']['cid']){
			$UnSee = $this->m("mail")->where("Sid=".$_SESSION['UserInfo']['cid']." AND See=0 And Gdel=0")->count();

		}else{
			$UnSee=0;
		}
		$this->s("_UnSee",$UnSee);

	}
	public function Login(){

		if(!$this->is_login() && \Chinax3::$conf["_action"] != 'LoginDo'){
			$this->v("Public/login");exit;
		}else{
			header("Location:http://wap.chnhuacai.com");
			// $this->success("您已登录", T('Index/Index'));
			
		}
	}
	protected function is_login(){
		$user = session("UserInfo");
		$ComomClass = array('Register');
		if(in_array(\Chinax3::$conf["_action"],$ComomClass)){
			return true;
		}
		if($user){
			$this->s('_me',$user);
			return $this->U = $user;
		}else{
			return false;
		}
	}
	public function GetCompany(){
		if($_SESSION['UserInfo']['cid']){
			$ComSee = $this->m("company")->where("Id=".$_SESSION['UserInfo']['cid'])->field("CompanyName,CompanyLogo")->find();
			$this->s("_ComSee",$ComSee);
		}
		
	}
	public function Logout(){
		unset($_SESSION['UserInfo']);
		header("Location:".$_SERVER['HTTP_REFERER']);
	}

}